<template>
  <div>
    <h3 align="center">购物车</h3>
    <goods
      v-for="(goods, index) in goodsList"
      :key="index"
      :goods="goods"
      @subGoods="subGoods"
      @addGoods="addGoods"
    ></goods>
    <div>商品的总价：{{ total }}</div>
  </div>
</template>

<script>
import { getGoodsList, getAddress, saveAddress, login } from '@/api/goods2'
import { weatherNow } from '@/api/weather'
import Goods from '@/components/Goods4.vue'
export default {
  components: {
    Goods,
  },

  data() {
    return { goodsList: [] }
  },

  computed: {
    total() {
      var sum = 0
      this.goodsList.forEach((goods) => {
        sum += goods.price * goods.num
      })
      return sum
    },
  },

  async mounted() {
    //获取网络请求后的数据,对goodsList进行赋值
    getGoodsList(2).then((res) => {
      console.log(res.data)
      this.goodsList = res.data
    })
    const resLogin = await login({
      username: '20050007',
      password: '123456',
    })
    // localStorage.setItem('token', resLogin.data.session_id)
    // const resAddress = await getAddress()
    // console.log(resAddress)
    // const address = {
    //   name: '软件2351',
    //   tel: '13813813813',
    //   area: '南京',
    //   detail: '南京市欣乐路188号',
    // }
    // const resSaveAddress = await saveAddress(address)
    // console.log(resSaveAddress)

    const resW = await weatherNow('六合')
    console.log(resW)
  },

  methods: {
    addGoods(id) {
      //从goodsList中找到goods，对num+1
      const goods = this.goodsList.find((item) => item.id == id)
      goods.num = goods.num + 1
    },
    subGoods(id) {
      //从goodsList中找到goods，对num-1
      const goods = this.goodsList.find((item) => item.id == id)
      goods.num = goods.num - 1
    },
  },
}
</script>

<style lang="scss" scoped>
</style>
